﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

namespace OMSReportViewer
{
    public partial class ReportSelector : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string[] files = Directory.GetFiles(PathLocations.Reports, "*.dat");
            
            foreach (string file in files)
            {
                m_dropQueries.Items.Add(Path.GetFileNameWithoutExtension(file));
            }
        }

        protected void m_btnConnect_Click(object sender, EventArgs e)
        {
            if (DatabaseDAO.Connect(m_txtConnectionString.Text))
            {
                Logger.LogInformation("Success: connect to database.");
            }
            else
            {
                Logger.LogInformation("Failed: connect to database.");
            }
        }

        protected void m_btnSelect_Click(object sender, EventArgs e)
        {
            StreamReader reader = new StreamReader(PathLocations.Reports + "\\" + m_dropQueries.SelectedValue + ".txt");
           
            try
            {
                m_txtQuery.Text = reader.ReadToEnd();
            }
            catch (Exception ex)
            {
                Logger.LogException(ex);
            }
            finally
            {
                reader.Close();
            }
        }
    }
}